![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // 连接成功,设置该连接属性
2 try
3 {
4 // 特殊处理连接的AutoCommit是否已经被设置
5 dbConn.setAutoCommit(true);
6 dbConn.setInUse();
7 DBSemaphore.unLock();
8 return dbConn;
9 }
10 catch (Exception ex)
11 {
12 ex.printStackTrace();
13 DBSemaphore.unLock();
14 return null;
15 }
View Code
新增DBSemaphore类属性及方法如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /**
2 * 数据库同步对象
3 * @author Damon
4 */
5 public class DBSemaphore
6 {
7 private static volatile boolean m_bInUse = false;
8
9 public DBSemaphore()
10 {}
11
12 /**
13 * 设置"使用标志"。 传入true表示请求“使用标志”,传入false表示释放“使用标志”。
14 * @param bNewValue boolean
15 * @return boolean
16 */
17 protected static synchronized boolean setInUseFlag(boolean bNewValue)
18 {
19 if (bNewValue == true)
20 {
21 // 请求“使用标志”
22 if (m_bInUse == true)
23 {
24 // “使用标志”已经被占用
25 return false;
26 }
27 else
28 {
29 m_bInUse = true;
30 return true;
31 }
32 }
33 else
34 {
35 // 释放“使用标志”
36 m_bInUse = false;
37 return true;
38 }
39 }
40
41 protected static void lock() throws Exception
42 {
43 lock(0);
44 }
45
46 protected static void lock(int nSeconds) throws Exception
47 {
48 if (nSeconds 0)
58 {
59 Thread.sleep(100);
60 }
61
62 if (nSeconds == 0)
63 {
64 throw new Exception("Lock time out");
65 }
66 }
67 }
68
69 protected static void unLock()
70 {
71 setInUseFlag(false);
72 }
73 }
View Code
到这里,整个配置处理结束了,更多的就需要在实际项目中发挥了~
|